package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.DeptDoctorDescRelDTO;
import com.pshcp.domain.model.DeptDoctorDescRel;
import com.pshcp.mapper.DeptDoctorDescRelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 科室介绍医生介绍关联 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2023-03-22
 */
@Service
public class DeptDoctorDescRelService {

    @Autowired
    private DeptDoctorDescRelMapper deptDoctorDescRelMapper;

    public PageList<DeptDoctorDescRel> list(DeptDoctorDescRelDTO deptDoctorDescRelDTO){
        if(deptDoctorDescRelDTO.getPageNum() != null && deptDoctorDescRelDTO.getPageSize() != null){
            PageHelper.startPage(deptDoctorDescRelDTO.getPageNum(), deptDoctorDescRelDTO.getPageSize());
        }
        Example example = new Example(DeptDoctorDescRel.class);
        Example.Criteria criteria = example.createCriteria();
        if(deptDoctorDescRelDTO.getStatus() != null){
            criteria.andEqualTo("status", deptDoctorDescRelDTO.getStatus());
        }else{
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        example.setOrderByClause(" created_time desc");
        List<DeptDoctorDescRel> list = deptDoctorDescRelMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DeptDoctorDescRel> select(DeptDoctorDescRel deptDoctorDescRel){
        return deptDoctorDescRelMapper.select(deptDoctorDescRel);
    }

    public int insert(DeptDoctorDescRel deptDoctorDescRel){
        deptDoctorDescRel.setId(SystemUtil.getId());
        deptDoctorDescRel.setStatus(CommonStatusEnum.INIT.getCode());
        deptDoctorDescRel.setCreatedTime(new Date());
        return deptDoctorDescRelMapper.insert(deptDoctorDescRel);
    }

    public int update(DeptDoctorDescRel deptDoctorDescRel){
        deptDoctorDescRel.setUpdateTime(new Date());
        return deptDoctorDescRelMapper.updateByPrimaryKeySelective(deptDoctorDescRel);
    }

    public DeptDoctorDescRel findById(Long id){
        return deptDoctorDescRelMapper.selectByPrimaryKey(id);
    }

    public List<DeptDoctorDescRel> findByIds(List<Long> ids){
        Example example = new Example(DeptDoctorDescRel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return deptDoctorDescRelMapper.selectByExample(example);
    }

    public List<DeptDoctorDescRel> findByDeptDescId(Long deptDescId){
        Example example = new Example(DeptDoctorDescRel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deptDescId", deptDescId);
        return deptDoctorDescRelMapper.selectByExample(example);
    }

    public List<DeptDoctorDescRel> findByDeptDescIdAndDoctorId(Long deptDescId, Long doctorId){
        Example example = new Example(DeptDoctorDescRel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deptDescId", deptDescId);
        criteria.andEqualTo("doctorId", doctorId);
        return deptDoctorDescRelMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(DeptDoctorDescRel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DeptDoctorDescRel deptDoctorDescRel = new DeptDoctorDescRel();
        deptDoctorDescRel.setStatus(CommonStatusEnum.DEL.getCode());
        return deptDoctorDescRelMapper.updateByExampleSelective(deptDoctorDescRel, example);
    }

}
